package com.server.llh.llhserverwork.utils;

import android.text.format.DateFormat;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2016/3/21.
 * 获取时间与时间换算
 */
public class TimeUtil {

    public static CharSequence formatLong(long duration){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.add(Calendar.MILLISECOND, (int) duration);
        Date time = calendar.getTime();
        int hour = (int) (duration / 1000 / 60 / 60);

        CharSequence format = (hour > 0 ) ? "kk:mm:ss" : "mm:ss";
        return DateFormat.format(format,time);
    }

    public static CharSequence formatInt(int duration){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.add(Calendar.MILLISECOND, duration);
        Date time = calendar.getTime();
        int hour =duration / 1000 / 60 / 60;

        CharSequence format = (hour > 0 ) ? "kk:mm:ss" : "mm:ss";
        return DateFormat.format(format,time);
    }

    /**
     * 获取当前时间
     * @return
     */
 public static String getTime(){
     SimpleDateFormat formatter    =   new    SimpleDateFormat    ("yyyy-MM-dd");
     Date    curDate    =   new    Date(System.currentTimeMillis());//获取当前时间
     String    str    =    formatter.format(curDate);
     return  str;
 }
    public static String setTimeData(){
        SimpleDateFormat formatter    =   new    SimpleDateFormat    ("HH:mm");
        Date    curDate    =   new    Date(System.currentTimeMillis());//获取当前时间
        String    str    =    formatter.format(curDate);
        return  str;
    }
    /**
     * 数据库时间转换
     * @return
     */
    public static String dateToStrLong( Date date) {

            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(date);
            return dateString;

    }

    /**
     * @param min
     * @return day hour min
     */
    public static String minConvertDayHourMin(int min){
        String  html="0分";
        if(min!=0){
            int m=(Integer) min;
            String format;
            Object[] array;
            Integer days =(int) (m/(60*24));
            Integer hours = (int) (m/(60)-days*24);
            Integer minutes = (int) (m-hours*60-days*24*60);
            if(days>0){
                format="%1$,d天%2$,d时%3$,d分";
                array=new Object[]{days,hours,minutes};
            }else if(hours>0){
                format="%1$,d时%2$,d分";
                array=new Object[]{hours,minutes};
            }else{
                format="%1$,d分";
                array=new Object[]{minutes};
            }
            html= String.format(format, array);
        }
        return html;
    }

    /**
     *
     * @param day
     * @param hour
     * @param min
     * @return min
     */
    public static String dayHourMinConvertMin(int day,int hour,int min){
        int days=day*24*60;
        int hours=hour*60;
        return days+"小时"+hours+"分钟"+min;
    }

    /**
     * 时间比较
     * @param startTime
     * @param stopTime
     * @return
     */
    public static boolean timeChack(String startTime,String stopTime){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            Date date1 = sdf.parse(startTime);
            Date date2 = sdf.parse(stopTime);
            if(date1.getTime()-date2.getTime()>0) {
                return false;
            }else{
                return true;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return true;
    }
    /**
     * 时间推算
     * @param data
     * @param time
     * @return
     */
    public static String setTimeDatas(String data, int time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sdf.parse(data);
            Calendar curr = Calendar.getInstance();
            curr.set(Calendar.MONTH, curr.get(Calendar.MONTH) - time);
            date = curr.getTime();
//			Calendar calendar = new GregorianCalendar();
//			calendar.setTime(date);
//			calendar.add(calendar.DATE, time);// 把日期往后增加一天.整数往后推,负数往前移动
//			date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
            String putDate = sdf.format(date); // 增加一天后的日期
            return putDate;
        } catch (Exception e) {
            System.out.println("exception==" + e.getMessage());
        }
        return "";
    }
}
